package com.mapapp.flutter_mapapp.widget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.Window;

import com.mapapp.flutter_mapapp.http.ProgressCancelListener;

import java.lang.ref.WeakReference;

public class SimpleLoadDialog {
    private ProgressDialog load = null;
    private Context context;
    private boolean cancelable;
    private ProgressCancelListener mProgressCancelListener;
    private final WeakReference<Context> reference;
    private String message = "加载中...";

    private MyCountDownTimer myCountDownTimer;

    public SimpleLoadDialog(Context context, ProgressCancelListener mProgressCancelListener,
                            boolean cancelable, String message) {
        super();
        this.reference = new WeakReference<Context>(context);
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
        this.message = message;
    }

    public SimpleLoadDialog(Context context, ProgressCancelListener mProgressCancelListener,
                            boolean cancelable) {
        super();
        this.reference = new WeakReference<>(context);
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
    }

    private class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if (load != null && load.isShowing() && context != null) {
                load.setCancelable(true);
            }
        }
    }

    private void create() {
        if (load == null) {
            context = reference.get();
            load = new ProgressDialog(context);
            load.setMessage(message);

//                    = new Dialog(context, R.style.loadstyle);
//            View dialogView = LayoutInflater.from(context).inflate(
//                    R.layout.custom_sload_layout, null);
            load.setCanceledOnTouchOutside(false);
            load.setCancelable(cancelable);
//            load.setContentView(dialogView);
            load.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    if (mProgressCancelListener != null)
                        mProgressCancelListener.onCancelProgress();
                }
            });
            Window dialogWindow = load.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setGravity(Gravity.CENTER_VERTICAL
                        | Gravity.CENTER_HORIZONTAL);
            }
        }
        if (!load.isShowing() && context != null) {
            if (!cancelable) {
                if (myCountDownTimer != null) {
                    myCountDownTimer.cancel();
                }
                myCountDownTimer = new MyCountDownTimer(20 * 1000, 1000);
                myCountDownTimer.start();
            }
            load.show();
        }
    }

    public void show() {
        create();
    }

    public void dismiss() {
        context = reference.get();
        if (load != null && load.isShowing() && !((Activity) context).isFinishing()) {
//            String name = Thread.currentThread().getName();
            if (myCountDownTimer != null) {
                myCountDownTimer.cancel();
            }
            load.dismiss();
            load = null;
        }
    }
}
